package com.example.cdpbookstore.web.servlet;

import com.example.cdpbookstore.pojo.entity.Customer;
import com.example.cdpbookstore.service.CustomerService;
import com.example.cdpbookstore.service.impl.CustomerServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    private final CustomerService customerService = new CustomerServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        Customer customer = customerService.login(username, password);
        if (customer != null) {
            req.getSession().setAttribute("customer", customer);
            req.setAttribute("page", "index.jsp");
            req.setAttribute("message", "登录成功");
            req.getRequestDispatcher("delay_jump.jsp").forward(req, resp);
        } else {
            req.setAttribute("loginErr", "用户名或密码错误");
            req.getRequestDispatcher("login.jsp").forward(req, resp);
        }
    }
}
